/////////////////////////////////////////////////////////////////////////
//
// Amuse Engine SDK - core/system/io/filesystems
// Copyright (c) 2014.  All Rights Reserved
//
// File:		AEDiskFileSystem.cpp
// Author:		Gianluca Belardelli
// Date:		09/10/2014
//
/////////////////////////////////////////////////////////////////////////
#include <AECore.h>

// Compression function for Merkle-Damgard construction.
// This function is generated using the framework provided.
#define Mix(h) (h) ^= (h) >> 23; \
				 (h) *= 0x2127599bf4325c37ULL; \
				 (h) ^= (h) >> 47

AEUINT64 fasthash64( const void *buf, size_t len, AEUINT64 seed)
{
        const AEUINT64    m = 0x880355f21e6d1965ULL;
        const AEUINT64 *pos = (const AEUINT64 *)buf;
        const AEUINT64 *end = pos + (len / 8);
        const unsigned char *pos2;
        AEUINT64 h = seed ^ (len * m);
        AEUINT64 v;

        while (pos != end) {
                v  = *pos++;
                h ^= Mix(v);
                h *= m;
        }

        pos2 = (const unsigned char*)pos;
        v = 0;

        switch (len & 7) {
        case 7: v ^= (AEUINT64)pos2[6] << 48;
        case 6: v ^= (AEUINT64)pos2[5] << 40;
        case 5: v ^= (AEUINT64)pos2[4] << 32;
        case 4: v ^= (AEUINT64)pos2[3] << 24;
        case 3: v ^= (AEUINT64)pos2[2] << 16;
        case 2: v ^= (AEUINT64)pos2[1] << 8;
        case 1: v ^= (AEUINT64)pos2[0];
                h ^= Mix(v);
                h *= m;
        }

        return Mix(h);
} 

AEDiskFileSystem::AEDiskFileSystem( const char *lpBasePath, AEBOOL32 bWritable ) : m_lpBasePath( lpBasePath )
{
	_buildDirLookup( m_lpBasePath, "" );
}

AEDiskFileSystem::~AEDiskFileSystem( void )
{
}

AEBOOL32 AEDiskFileSystem::FileExists( const char *lpPath )
{
	AEUINT64 hash = fasthash64( lpPath, strlen( lpPath ), 17061973 );
	if( m_arrFilesInDirectory.IndexOf( hash ) > -1 )
		return 1;

	return 0;
}

AEBOOL32 AEDiskFileSystem::IsWritable( void ) const
{
	return 1;
}
